<!--
 * @Description: 秒杀商品批量添加
 * @Author: Li Yujie
 * @Date: 2020/12/31
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2020/12/31
-->
<template>
    <div class="wrapper">
        <div class="page">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    批量添加商品
                </div>
            </div>
            <!-- /header -->
            <!-- table -->
            <el-form
                :model="ruleForm"
                :rules="rules"
                style="height: calc(100% -  100px)"
                ref="ruleForm"
            >
                <el-table
                    :data="ruleForm.tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="tempLateName"
                    @select-all="handleSelectAll"
                >
                    <el-table-column
                        align="center"
                        type="selection"
                        width="80"
                    >
                        <template slot-scope="scope">
                            <div
                                v-for="item in scope.row.product_list"
                                :key="item.product_no"
                                class="table-item"
                            >
                                <el-checkbox
                                    v-model="item.check_status"
                                    :true-label="1"
                                    :false-label="0"
                                />
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="商品信息"
                        width="280"
                        fixed="left"
                    >
                        <template slot-scope="scope">
                            <div class="goods-info">
                                <img
                                    :src="scope.row.goods_cover"
                                    class="goods-cover"
                                    @click="fnShowPreview(scope.row.goods_cover)"
                                >
                                <div class="info">
                                    <div class="goods-info__title">
                                        {{ scope.row.goods_title_origin }}
                                    </div>
                                    <div class="goods-info__no">
                                        商品编号:{{ scope.row.goods_no }}
                                    </div>
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="商品标题"
                        width="180"
                        show-overflow-tooltip
                    >
                        <template slot-scope="scope">
                            <div class="table-item">
                                <el-form-item
                                    :prop="`tableData.${scope.$index}.goods_title`"
                                    :rules="rules.goods_title"
                                >
                                    <el-input
                                        v-model="scope.row.goods_title"
                                        clearable
                                        size="small"
                                    />
                                </el-form-item>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="本场限购数量"
                        show-overflow-tooltip
                        width="180"
                    >
                        <template slot-scope="scope">
                            <div class="table-item">
                                <el-form-item
                                    :prop="`tableData.${scope.$index}.buy_limit_count`"
                                    :rules="rules.buy_limit_count"
                                >
                                    <el-input
                                        v-model="scope.row.buy_limit_count"
                                        placeholder="-1为不限制"
                                        clearable
                                        size="small"
                                    />
                                </el-form-item>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="商品规格"
                        align="center"
                        width="300"
                    >
                        <template slot-scope="scope">
                            <div
                                class="table-item"
                                v-for="(item, index) in scope.row.product_list"
                                :key="item.product_no"
                                :style="`color:${item.check_status ? '#1890FF' : ''};cursor: pointer`"
                                @click="handleChecked(scope.row, scope.$index, index)"
                            >
                                <span
                                    v-for="(option,i) in item.spec_list"
                                    :key="i"
                                    style="margin: 0 6px"
                                >
                                    {{ `${option.spec_name} : ${option.spec_value}` }}
                                </span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="原售价"
                        show-overflow-tooltip
                        width="180"
                    >
                        <template slot-scope="scope">
                            <div
                                class="table-item"
                                v-for="item in scope.row.product_list"
                                :key="item.product_no"
                            >
                                ￥{{ item.product_price }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="秒杀价"
                        width="150"
                    >
                        <template slot-scope="scope">
                            <div
                                class="table-item"
                                v-for="(item,index) in scope.row.product_list"
                                :key="item.product_no"
                            >
                                <el-form-item
                                    :prop="`tableData.${scope.$index}.product_list[${index}]`"
                                    :rules="rules.price"
                                >
                                    <el-input
                                        v-model="item.price"
                                        clearable
                                        size="small"
                                        :disabled="!scope.row.product_list[index].check_status"
                                        @click.native="clickDisableInput(scope.row, scope.$index, index)"
                                    />
                                </el-form-item>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="活动库存"
                        width="150"
                    >
                        <template slot-scope="scope">
                            <div
                                class="table-item"
                                v-for="(item,index) in scope.row.product_list"
                                :key="item.product_no"
                            >
                                <el-form-item
                                    :prop="`tableData.${scope.$index}.product_list[${index}]`"
                                    :rules="rules.stock"
                                >
                                    <el-input
                                        oninput="value = value.replace(/[^\d]/g,'')"
                                        v-model.number="item.stock"
                                        clearable
                                        size="small"
                                        :disabled="!scope.row.product_list[index].check_status"
                                        @click.native="clickDisableInput(scope.row, scope.$index, index)"
                                    />
                                </el-form-item>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="商家设置库存"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <div
                                class="table-item"
                                v-for="item in scope.row.product_list"
                                :key="item.product_no"
                            >
                                {{ item.product_stock }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop=""
                        label="排序号"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <div class="table-item">
                                <el-form-item
                                    :prop="`tableData.${scope.$index}.sort`"
                                    :rules="rules.sort"
                                >
                                    <el-input
                                        oninput="value = value.replace(/[^\d]/g,'')"
                                        v-model.number="scope.row.sort"
                                        clearable
                                        size="small"
                                    />
                                </el-form-item>
                            </div>
                        </template>
                    </el-table-column>
                    <!--<el-table-column
                        prop=""
                        label="活动状态设置"
                        align="center"
                        width="200px"
                    >
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.goods_status_vip"
                                active-text="上架"
                                inactive-text="下架"
                                :active-value="1"
                                :inactive-value="0"
                            />
                        </template>
                    </el-table-column>-->
                </el-table>
            </el-form>
            <!-- /table -->
            <div class="btn-group">
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="handleCancel()"
                >
                    取消
                </el-button>
                <el-button
                    size="medium"
                    type="primary"
                    style="width: 120px"
                    @click="handleSubmit()"
                >
                    完成
                </el-button>
            </div>
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview"/>
    </div>
</template>

<script>
/**
 * 页面接收参数
 * @param goodsList - 所选商品列表
 */

import {back} from '@/mixin/back';
import imgPreview from '@/components/common/img-preview';

export default {
    name: "SeckillGoodsAddBatch",
    data() {
        const validatePrice = (rule, value, callback) => {

            if (value.check_status && !value.price) {
                callback(new Error('请输入秒杀价'));
            } else {
                callback();
            }
        };

        const validateStock = (rule, value, callback) => {

            if (value.check_status && !value.stock && value.stock !== 0) {
                callback(new Error('请输入活动库存'));
            } else {
                callback();
            }
        };

        return {
            ruleForm: {
                tableData: [],
            },
            time_no: '',
            rules: {
                goods_title: [
                    {required: true, message: '请输入商品标题', trigger: 'blur'}
                ],
                price: [
                    {validator: validatePrice, trigger: 'blur'},
                ],
                stock: [
                    {validator: validateStock, trigger: 'blur'},
                ],
                sort: [
                    {required: true, message: '请输入排序号', trigger: 'blur'}
                ],
                buy_limit_count: [
                    {required: true, message: '请输入本场限购数量', trigger: 'blur'}
                ]
            }
        };
    },
    mixins: [back],
    components: {
        imgPreview
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '活动管理'}, {title: '限时秒杀'},
            {title: '活动排期', path: '/student/seckill-schedule-list'}, {title: '批量添加商品'}]);
        let query = this.$route.query;

        this.time_no = query.time_no;
        this.init();
    },

    methods: {

        init() {
            let goodsList = JSON.parse(this.$route.query.goodsList);

            goodsList.map(item => {
                item.goods_title_origin = item.goods_title;
                item.sort = 99;
                item.buy_limit_count = 1;
                item.product_list.map(subItem => {
                    subItem.price = '';
                    subItem.stock = '';
                    subItem.check_status = 0;
                });
            });
            this.ruleForm.tableData = goodsList;
        },
        /**
         * 查看图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         */
        fnShowPreview(images) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },
        /**
         * 取消返回列表
         */
        handleCancel() {
            this.$zdConfirm('返回后将不会保存您所修改的内容,是否返回?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.handleRoute('seckillScheduleList');
            });
        },

        /**
         * 提交数据
         */
        handleSubmit() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let tableData = [...this.ruleForm.tableData];

                    tableData.forEach(item => {
                        item.product_info = item.product_list.filter((item, i) => {
                            return item.check_status !== 0;
                        });
                    });
                    let params = {
                        goods_json: JSON.stringify(tableData),
                        time_no: this.time_no
                    };

                    this.$post('/student/shop_goods_flash_sale%5Cadd_batch', params, resp => {
                        if (resp.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '保存成功',
                                type: 'success',
                                duration: 2000
                            });
                            setTimeout(() => {
                                this.handleRoute('seckillScheduleList');
                            }, 500);
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                duration: 2000,
                                type: 'warning'
                            });
                        }
                    });
                }
            });
        },

        /**
         * 点击规格选中当前规格
         * @param row
         * @param rowIndex
         * @param index
         */
        handleChecked(row, rowIndex, index) {
            if (row.product_list[index].check_status) {
                row.product_list[index].check_status = 0;
            } else {
                row.product_list[index].check_status = 1;
            }
        },

        /**
         * 点击禁用的input
         * @param row
         * @param rowIndex
         * @param index
         */
        clickDisableInput(row, rowIndex, index) {
            if (!row.product_list[index].check_status) {
                this.$message.warning('请勾选该规格');
            }
        },

        /**
         * 点击全选
         * @param val
         */
        handleSelectAll(val) {
            this.ruleForm.tableData.map(item => {
                item.product_list.map(subItem => {
                    if (val.length) {
                        subItem.check_status = 1;
                    } else {
                        subItem.check_status = 0;
                    }
                });
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },
    }
};
</script>

<style scoped lang="less">

.wrapper {
    width: 100%;
    position: relative;
    background: #fff;
    box-sizing: border-box;

    .page {
        width: 100%;
        height: calc(100% - 60px);
        background: #fff;
        display: flex;
        flex-direction: column;
        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        .btn-group {
            text-align: center;
            margin-top: 24px;

            .el-button {
                margin: 0 24px;
            }
        }

        .table-item {
            margin: 28px 0;
            height: 30px;
        }
    }

    .goods-info {
        display: flex;
        align-items: center;

        .goods-cover {
            width: 80px;
            height: 80px;
            cursor: pointer;
            margin-right: 12px;
            flex-shrink: 0;
            object-fit: cover;
        }

        .info {
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;

            .goods-info__title {
                font-size: 14px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 2;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                margin-bottom: 12px;
            }
        }
    }
}
</style>
